<?php

//  openssl x509 -text  -noout -in  test/client.crt
//  curl -k --cert client.pem --key key.pem https://www.xxxx.com
//  curl -k --cert "test\client.crt" --key "test\client.key" https://server.com:1216/rpc
//  curl -k --cert "C:\workspace\Go\src\goc\jiami\keys\2\client.crt" --key "C:\workspace\Go\src\goc\jiami\keys\2\client.key" https://server.com:1216/rpc


function curl_post($url, $data, $header = '', $timeOut = 1)
{
    //初始化curl
    $ch = curl_init();
    //参数设置
    curl_setopt($ch, CURLOPT_URL, $url);

    //
    // 在这里设定
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSLCERT , "test/client.crt");
    curl_setopt($ch, CURLOPT_SSLKEY, "test/client.key");
    // 在这里设定
    //

    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if($header != '') {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    $result = curl_exec($ch);
    //连接失败
    if($result == FALSE) {
        echo '[ CURL ] ERROR ' . curl_error($ch)."\n";
    }
    curl_close($ch);
    return $result;
}


function rpcCall($method, $params)
{
    $go_url_rpc =  "https://server.com:1216/rpc";
    $url = $go_url_rpc;

    $data['jsonrpc'] = "2.0";
    $data['method'] = $method;
    $data['params'] = $params;
    $data['id'] = 2;

    $postData = json_encode($data);
    //Content-Type: application/json
    $headers = [
        'Content-Type: application/json; charset=utf-8',
    ];
    $ret = curl_post($url, $postData, $headers);
    return $ret;
}

